class DevServer {
  async setup() {
    await this.startServer();
  }

  async startServer() {
    const webpack = require("webpack");
    const webpackDevServer = require("webpack-dev-server");
    const { getConfig } = require("../config");
    const config = getConfig()
    // compiler：是 Webpack 编译器实例，它控制着整个构建生命周期。
    /**
     * compiler.run()：启动编译
     * compiler.watch()：启动监听编译
     */
    const compiler = webpack(config);
    // 创建 webpack 开发服务器
    this.server = new webpackDevServer(config.devServer, compiler);
    this.server.start();
  }
}

module.exports = new DevServer();
